<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" +
            request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>后台登录</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script type="text/javascript" src="/static/js/jquery-2.1.1.min.js"></script>
    <script src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>

</head>
<body class="login-bg">

<div class="login">
    <div class="message">用户登录</div>
    <div id="darkbannerwrap"></div>
    <hr class="hr15">

    <form action="" method="post" class="layui-form">
        <input name="uAccount" placeholder="账号" id="uAccount" type="text" class="layui-input"
               onblur="login_checkAccount()">
        <p id="notice_account" style="color: red"></p>
        <hr class="hr15">
        <input name="uPassword" placeholder="密码" id="uPassword" type="password" class="layui-input">
        <hr class="hr15">
        <input value="登录" style="width:100%;" type="button" onclick="login()">
        <hr class="hr20">
        <button type="button" class="layui-btn layui-btn-normal" onclick="register()">注册</button>
        <hr class="hr20">
    </form>
</div>
<script>
    var flagAccount = false;

    function login() {
        if (flagAccount == true) {
            $.ajax({
                url: "/user/login",
                type: "post",
                dataType: "json",
                data: {
                    uAccount: $("#uAccount").val(),
                    uPassword: $("#uPassword").val()
                },
                success: function (data) {
                    console.log(data)
                    if (data.code == 200) {
                        layer.alert("欢迎" + data.loginUser.uaccount + "登陆", {icon: 6}, function () {
                            location.href = "/sys/goIndex?uAccount="+ data.loginUser.uaccount;
                        })
                    } else if (data.code == 201) {
                        layer.alert("欢迎登陆,但是非管理员功能未开放", {icon: 6}, function () {
                            location.href = "/sys/goIndex"
                        })
                    }else {
                        layer.alert(data.message, {icon: 5},)
                    }

                }
            })
        } else {
            login_checkAccount();
        }
    }

    function register() {
        location.href = "/sys/goRegister"
    }

    function login_checkAccount() {
        let validPattern = /^[\u4E00-\u9FA5A-Za-z0-9_]+$/;
        let uAccount = $("#uAccount").val();

        flagAccount = validPattern.test(uAccount);

        if (uAccount.length < 4) {
            $("#notice_account").text("账号不得小于4位");
            flagAccount = false;
        } else if (flagAccount != true) {
            $("#notice_account").text("请输入中、英、数字、下划线组成的账号");
            flagAccount = false;
        } else {
            $("#notice_account").text("");
        }
    }
</script>
</body>
</html>
